class EtapasController < ApplicationController


  # GET /etapas
  # GET /etapas.xml
 def index

 @or=params[:id]

#  @etapas variable que almacena los datos de las etapas
#   de una orden pra poder listarla y conocer el estatus

    @etapas = Etapa.find_by_sql("select distinct e.fecha, e.lugar as lugar, e.descripcion, o.estatus,o.traking,o.fecha_orden from etapas e, orden_servicios o, clientes c where e.orden_servicio_id=o.id and e.orden_servicio_id='"+@or+"'")
        respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @etapas }
    end


  end

  # GET /etapas/1
  # GET /etapas/1.xml
  def show
    @etapa = Etapa.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @etapa }
    end
  end

  # GET /etapas/new
  # GET /etapas/new.xml
  def new
    @etapa = Etapa.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @etapa }
    end
  end

  # GET /etapas/1/edit
  def edit
    @etapa = Etapa.find(params[:id])
  end

  # POST /etapas
  # POST /etapas.xml
  def create
    @etapa = Etapa.new(params[:etapa])

    respond_to do |format|
      if @etapa.save
        flash[:notice] = 'Etapa was successfully created.'
        format.html { redirect_to(@etapa) }
        format.xml  { render :xml => @etapa, :status => :created, :location => @etapa }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @etapa.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /etapas/1
  # PUT /etapas/1.xml
  def update
    @etapa = Etapa.find(params[:id])

    respond_to do |format|
      if @etapa.update_attributes(params[:etapa])
        flash[:notice] = 'Etapa was successfully updated.'
        format.html { redirect_to(@etapa) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @etapa.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /etapas/1
  # DELETE /etapas/1.xml
  def destroy
    @etapa = Etapa.find(params[:id])
    @etapa.destroy

    respond_to do |format|
      format.html { redirect_to(etapas_url) }
      format.xml  { head :ok }
    end
  end


  def showtracking

    @trackingid = params[:tracking]
    @etapa = Etapa.find_by_sql("SELECT E.FECHA as fecha, E.LUGAR as lugar, E.DESCRIPCION as descripcion FROM ORDEN_SERVICIOS O, ETAPAS E WHERE E.ORDEN_SERVICIO_ID = O.ID AND O.TRAKING = '"+@trackingid.to_s+"' order by e.fecha desc")
    #    @etapa = Etapa.first(:conditions => ["traking = ? ",params[:trackingid]])
    @estatus = OrdenServicio.find(:first, :conditions  => ["traking = ?", @trackingid])
    redirect_to "showtracking"
    respond_to do |format|
      format.html # showtracking.html.erb
      format.xml  { render :xml => @etapa }
    end
  end


end
